package com.linchong.linkedlist.easy;

import com.linchong.linkedlist.basic.BasicLinkList;

/**
 * @author linchong
 * @version 1.0
 * @Date: 2020-11-30 19:47
 * @Description: ReverseList$反转一个单链表。
 * <p>
 * 示例:
 * <p>
 * 输入: 1->2->3->4->5->NULL
 * 输出: 5->4->3->2->1->NULL
 * 进阶:
 * 你可以迭代或递归地反转链表。你能否用两种方法解决这道题？
 * <p>
 * 链接：https://leetcode-cn.com/problems/reverse-linked-list
 */
@SuppressWarnings("unused")
public class ReverseList extends BasicLinkList {



	public ListNode reverseList2(ListNode head) {
		if (head == null) {
			return null;
		}

		ListNode node = head;
		ListNode res = null;

		while (node != null) {
			ListNode listNode = new ListNode(node.val);
			listNode.next = res;
			res = listNode;
			node = node.next;
		}
		return res;

	}

	public ListNode reverseList(ListNode head) {
		if (head == null) {
			return null;
		}
		ListNode res = null;

		while (head != null) {
			ListNode next = head.next;
			head.next = res;
			res = head;
			head = next;
		}
		return res;
	}


}
